<?php
require_once 'Zend/Http/Client.php';
require_once('simplehtmldom/simple_html_dom.php');

//$url = "http://pgb.home.pl/pgb/baza_testujaca_serwer";
$url = "http://178.217.184.110/pgb";
$url .= $_SERVER['REQUEST_URI'];
$url = str_replace('pgb_mirror/', '', $url);

$client = new Zend_Http_Client($url, array('keepalive' => true));
$client->resetParameters();

// Do we have the cookies stored in our session?
if (isset($_SESSION['cookiejar']) && $_SESSION['cookiejar'] instanceof Zend_Http_CookieJar) {
  $client->setCookieJar($_SESSION['cookiejar']);
}
else {
  $client->setCookieJar();
}

$is_post = !empty($_POST);

if ($is_post) {
  $client->setParameterPost($_POST);
}

$type = ($is_post) ? Zend_Http_Client::POST : Zend_Http_Client::GET;
$response = $client->request($type);


// Store cookies in session, for next page
$_SESSION['cookiejar'] = $client->getCookieJar();


echo sprintf("Mirror: %s, status: %s\n<br>", $url, $response->getMessage());

$html = str_get_html($response->getBody());
$plain = $html->plaintext;
$zfdebug_output = strpos($plain, 'ZFDebug');

if ($zfdebug_output !== false) {
  $plain = substr($plain, 0, $zfdebug_output);
}

echo $plain;

//echo ($response->getBody());

// clean up memory
$html->clear();
unset($html);

